package leader.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import leader.service.DirectTreatiseService;

import com.opensymphony.xwork2.ActionContext;

import teacher.bean.Award;
import teacher.bean.Teacher;
import teacher.bean.Treatise;

public class DirectTreatiseLikeNameAction {
	private List<Treatise> treatise;
	private String treName;
	private int currentpage;
	private int endpage;
	private int count;
	private int pagesize;
	private int flag;
	private int code;
	public List<Treatise> getTreatise() {
		return treatise;
	}
	public void setTreatise(List<Treatise> treatise) {
		this.treatise = treatise;
	}
	public String getTreName() {
		return treName;
	}
	public void setTreName(String treName) {
		this.treName = treName;
	}
	
	public int getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public int getEndpage() {
		return endpage;
	}
	public void setEndpage(int endpage) {
		this.endpage = endpage;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String execute() {	
		if(this.getCode()==1) {
			try {
				treName = new String(treName.getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Teacher lea=(Teacher) ActionContext.getContext().getSession().get("leader");
		DirectTreatiseService tre = new DirectTreatiseService();
		pagesize = 10;
		count = tre.getCountOfDirectTreatiseByName(lea, treName);
    	List<Award> awards = new ArrayList<Award>();
    	if(currentpage == 0) {
    		currentpage = 1;
    	}
    	int add = 0;
    	if(count%pagesize != 0) {
    		add = 1;
    	}
    	endpage = count/pagesize + add;
    	if(currentpage > endpage && endpage > 0) {
    		currentpage = endpage;
    	}
    	ActionContext.getContext().put("flag",2);
    	flag = 2;
		treatise = tre.selectSegregateTreatiseLikeName(treName, lea, (currentpage-1)*pagesize+1, pagesize);
		return "success";
	}
}
